(function(window, angular, undefined){
	'use strict';
	var control = angular.module('control', ['ui.bootstrap']);
	var config = {
		moduleName : control.name,
		tpl_folder : 'template'
	};
	var api = {
		user : 'users/users/user',
		login: 'users/users/login',
		logout :'users/users/logout'
	};
	var tpl_path = [rootConfig.moduleDir,config.moduleName,config.tpl_folder].join('/');
	var baseState = [config.moduleName].join('');
	var baseController = [config.moduleName].join('');
	var indexState = [config.moduleName,'index'].join('.');
	var indexController = [config.moduleName,'index'].join('.');
	control.config(function ($stateProvider, $urlRouterProvider) {
		$stateProvider
			.state(baseState,{
				//abstract : true,
		    	url : '/control',
		    	templateUrl : [tpl_path,'main.html'].join('/'),
		    	controller: baseController
		    })
		    .state(indexState,{
		    	url :'/index',
		    	templateUrl : [tpl_path,'index.html'].join('/'),
		    	controller: indexController
		    });
	});
	control.controller(baseController, ['$scope','rootService','$state','$http','$location','ControlService', 
		function ($scope,rootService,$state,$http,$location,ControlService)
	{
		$scope.reload = function(){
			$state.reload();// config in rootApp
		}
	}]);
	control.controller(indexController, ['$scope','rootService','$state','$http','$location','ControlService','$modal','$filter',
	 function ($scope,rootService,$state,$http,$location,ControlService,$modal,$filter) {
		/* Load all  table-cafe list */
		ControlService.getTableCafe().success(function(res){
			$scope.tableCafe = res.data;
		});
		ControlService.getEmployee().success(function(res){
			$scope.employee = res.data;
		});
		/* ModalDrinkList area */
		$scope.resultChoose = [];
		/* Load data for ModalDrinkList */
		ControlService.getListDrink().success(function(res){
			$scope.items = res.data;
		});
		$scope.click = function(){
			myModal();
		}
		/* order drink*/
		$scope.order = function(item){
			/* load drink list */
			$scope.addOrder = item;
			$scope.arrayResult = [];
			$scope.resultChoose = [];
			$('#modalDrinkList').modal('show')
			
		}
		$scope.SelectDrink = function(key){
			var item = $scope.items[key];
			$scope.resultChoose.push(item);
			/* Filter resultChoose */
			var inarray = [];
			//$scope.arrayResult = [];
			/* prepare data */
			$scope.resultChoose = $scope.resultChoose.map(function(item){
				item.count = 0;
				return item;
			});
			$scope.resultChoose.forEach(function(item){
				if($.inArray(item,inarray)==-1){
					inarray.push(item);
				}
			});
			$scope.resultChoose.forEach(function(item){
				inarray.forEach(function(data,i){
					if(item.id==data.id){
						inarray[i].count += 1;
					}
				});
			});
			$scope.arrayResult = inarray;
		}
		$scope.deleteArrayResult = function(key,item){
			$scope.arrayResult.splice(key,1);
			$scope.resultChoose.forEach(function(v,k){
				if(item.id == v.id){
					$scope.resultChoose.splice(k,1);
				}
			});
		}
		var parseData = function(data){
			var result = [];
			data.forEach(function(item,key){
				result.push([item.id,item.count].join('_'));
			});
			return result.join(',');

		}
		$scope.formData = {
			employeeChoose : ''
		}
		$scope.saveForm = function(){
			$('#modalDrinkList').modal('hide')
			/* parseData */
			var data = {
				drink_id : parseData($scope.arrayResult),
				table_id : $scope.addOrder.id,
				employee_id : $scope.formData.employeeChoose
			}
			//miss valid form
			ControlService.order(data).success(function(res){
				console.log(res);
				$scope.reload();
			});
		}
		/*--------------------------------------------*/
		//paybill area
		$scope.payBill = function(item){
			$scope.billItem = [];
			$scope.payBillItem = item;
			ControlService.getBill({table_id:item.id}).success(function(res){
				if(res.data.length < 1) return;
				$scope.billItem = res.data;
				$scope.totalMoney = 0;
				res.data.forEach(function(item){
					$scope.totalMoney+=(item.price*item.quantum);
				});
				$('#modalPayBill').modal('show');
			});
		}
		/* orderBy table items */
		$scope.$parent.orderBy = function(predicate){
			$scope.tableCafe = $filter('orderBy')($scope.tableCafe,predicate);
		}

	}]);
	control.service('ControlService', ['$http',function ($http) {
		return {
			getTableCafe : function(param){
				var url = [rootConfig.baseUrl,'control/control','table'].join('/');
				return $http.get(url);
			},
			getListDrink : function(){
				var url = [rootConfig.baseUrl,'drink/drink','list'].join('/');
				return $http.get(url);
			},
			getEmployee : function(){
				var url = [rootConfig.baseUrl,'employee/employee','list'].join('/');
				return $http.get(url);
			},
			order : function(data){
				var url = [rootConfig.baseUrl,'control/control','order'].join('/');
				return $http.post(url,data);
			},
			getBill : function(data){
				var url = [rootConfig.baseUrl,'control/control','bill'].join('/');
				return $http.post(url,data);
			}
		};
	}]);
	control.directive('modalDrinkList',function(){
		return {
			restrict : 'E',
			replace:true,
			templateUrl : [tpl_path,'ModalDrinkList.html'].join('/'),
			scope : true,
		}
	});
	control.directive('modalPayBill',function(){
		return {
			restrict : 'E',
			replace  : true,
			templateUrl : [tpl_path,'modalPayBill.html'].join('/'),
			scope : true,
		}
	});
})(window, window.angular);